import {getToken, getUser} from "./utils/auth";
import router from "./router";
import VueRouter from "vue-router";

router.beforeEach(async (to, from, next) => {
    const hasToken = getToken()
    const user = getUser();
    if (hasToken) {
        //存在token则可以访问人也界面
        next()
    } else {
        //为需要拦截的页面的路由添加 meta:{role:''}
        if (to.meta.role) {
            next(`/login`)
        } else {
            next()
        }
    }

})
router.afterEach(() => {

})

//路由守卫报错解决
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
}

